<?php

namespace app\api\controller;

use Throwable;
use think\Response;
use app\common\library\Upload;
use app\common\controller\Frontend;
use Afaya\EdgeTTS\Service\EdgeTTS;
use app\admin\model\GameWords;
class Ajax extends Frontend
{
    protected array $noNeedLogin = ['area', 'buildSuffixSvg','audio','tts'];

    protected array $noNeedPermission = ['upload'];

    public function initialize(): void
    {
        parent::initialize();
    }

    public function upload(): void
    {
        $file   = $this->request->file('file');
        $driver = $this->request->param('driver', 'local');
        $topic  = $this->request->param('topic', 'default');
        try {
            $upload     = new Upload();
            $attachment = $upload
                ->setFile($file)
                ->setDriver($driver)
                ->setTopic($topic)
                ->upload(null, 0, $this->auth->id);
            unset($attachment['create_time'], $attachment['quote']);
        } catch (Throwable $e) {
            $this->error($e->getMessage());
        }

        $this->success(__('File uploaded successfully'), [
            'file' => $attachment ?? []
        ]);
    }

    /**
     * 省份地区数据
     * @throws Throwable
     */
    public function area(): void
    {
        $this->success('', get_area());
    }

    public function buildSuffixSvg(): Response
    {
        $suffix     = $this->request->param('suffix', 'file');
        $background = $this->request->param('background');
        $content    = build_suffix_svg((string)$suffix, (string)$background);
        return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/svg+xml');
    }
    protected function tts($text,$voice){
        $output_file = 'output_' . time().mt_rand(1000,9000);
        if($voice == 'en'){
            $ssml = '<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="en-US">
                <voice name="en-US-AriaNeural">
                    <prosody rate="0%" pitch="0Hz" volume="0%">
                        ' . $text . '
                    </prosody>
                </voice>
            </speak>';
            $voice = 'en-US-AriaNeural';
        }else{
            $ssml = '<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="zh-CN">
                <voice name="zh-CN-XiaoxiaoNeural">
                    <prosody rate="0%" pitch="0Hz" volume="0%">
                        '.$text.'
                    </prosody>
                </voice>
            </speak>';
            $voice = 'zh-CN-XiaoxiaoNeural';
        }
        $tts = new EdgeTTS();
        $tts->synthesize($ssml, $voice, [
            'rate' => '0%',
            'volume' => '0%',
            'pitch' => '0Hz'
        ]);
        $tts->toFile("{$output_file}");
        return $output_file.'.mp3';
    }
    public function audio(){
        for($i=0; $i<60;$i++){
            $words = GameWords::where('speak_is',0)->find();
            $words->speak_en = $this->tts($words['en'],'en');
    
            $words->speak_cn = $this->tts($words['cn'],'cn');
            $words->speak_is =1;
            
            $words->save();
            usleep(500*1000);
        }
        
    }
}